package netpacker

import (
	"bytes"
	"encoding/binary"
)

type NetPackerSt struct {
	buffer *bytes.Buffer
}

//初始化生成一个Packer
func NewNetPacker() *NetPackerSt {
	packer := &NetPackerSt{bytes.NewBuffer(nil)}
	return packer
}

//写入一个32位-4字节整型
func (self *NetPackerSt) WriteInt32(n32 int32) error {
	if err := binary.Write(self.buffer, binary.BigEndian, n32); err != nil {
		return err
	}
	return nil
}

//读取一个32位-4字节整型
func (self *NetPackerSt) ReadInt32() (n32 int32, err error) {
	err = binary.Read(self.buffer, binary.BigEndian, &n32)
	return
}

//写入一个16位-2字节整型
func (self *NetPackerSt) WriteInt16(n16 int16) error {
	if err := binary.Write(self.buffer, binary.BigEndian, n16); err != nil {
		return err
	}
	return nil
}

//读取一个16位-2字节整型
func (self *NetPackerSt) ReadInt16() (n16 int16, err error) {
	err = binary.Read(self.buffer, binary.BigEndian, &n16)
	return
}

//写入一个8位整型
func (self *NetPackerSt) WriteInt8(n8 int8) error {
	if err := binary.Write(self.buffer, binary.BigEndian, n8); err != nil {
		return err
	}
	return nil
}

//读取一个8位-1字节整型
func (self *NetPackerSt) ReadInt8() (n8 int8, err error) {
	err = binary.Read(self.buffer, binary.BigEndian, &n8)
	return
}

//写入一个字节信息
func (self *NetPackerSt) WriteByte(c byte) error {
	return self.buffer.WriteByte(c)
}

//读取一个字节信息
func (self *NetPackerSt) ReadByte() (c byte, err error) {
	c, err = self.buffer.ReadByte()
	return
}

//写入多字节留数据信息4+bytes
func (self *NetPackerSt) WriteBytes(buf []byte) (nlen int, err error) {
	nlen = len(buf)
	self.WriteInt32(int32(nlen))
	nlen, err = self.buffer.Write(buf)
	return
}

//写入多字节留数据信息4+bytes
func (self *NetPackerSt) ReadBytes() (buf []byte, err error) {
	nlen, _ := self.ReadInt32()
	buf = make([]byte, nlen)
	_, err = self.buffer.Read(buf)
	return
}

//写入一个字符串，最长65535
func (self *NetPackerSt) WriteStr(str string) (nlen int, err error) {
	nlen = len(str)
	self.WriteInt16(int16(nlen))
	nlen, err = self.buffer.WriteString(str)
	return
}

//读取字符串，最长65535
func (self *NetPackerSt) ReadStr() (str string, err error) {
	nlen, _ := self.ReadInt16()
	buf := make([]byte, nlen)
	_, err = self.buffer.Read(buf)
	str = string(buf)
	return
}

//写入一个字符串，最长255
func (self *NetPackerSt) WriteShortStr(str string) (nlen int, err error) {
	nlen = len(str)
	self.WriteInt8(int8(nlen))
	nlen, err = self.buffer.WriteString(str)
	return
}

//读取字符串，最长255
func (self *NetPackerSt) ReadShortStr() (str string, err error) {
	nlen, _ := self.ReadInt8()
	buf := make([]byte, nlen)
	_, err = self.buffer.Read(buf)
	str = string(buf)
	return
}

//写入一个字符串，最长4个字节的长度
func (self *NetPackerSt) WriteLongStr(str string) (nlen int, err error) {
	nlen = len(str)
	self.WriteInt32(int32(nlen))
	nlen, err = self.buffer.WriteString(str)
	return
}

//写入一个字符串，最长4个字节的长度
func (self *NetPackerSt) ReadLongStr() (str string, err error) {
	nlen, _ := self.ReadInt32()
	buf := make([]byte, nlen)
	_, err = self.buffer.Read(buf)
	str = string(buf)
	return
}

//写入占位8字节的浮点数
func (self *NetPackerSt) WriteFloat64(f64 float64) error {
	if err := binary.Write(self.buffer, binary.BigEndian, f64); err != nil {
		return err
	}
	return nil
}

//读取占位8字节的浮点数
func (self *NetPackerSt) ReadFloat64() (f64 float64, err error) {
	err = binary.Read(self.buffer, binary.BigEndian, &f64)
	return
}

//写入占位4字节的浮点数
func (self *NetPackerSt) WriteFloat32(f32 float32) error {
	if err := binary.Write(self.buffer, binary.BigEndian, f32); err != nil {
		return err
	}
	return nil
}

//读取占位4字节的浮点数
func (self *NetPackerSt) ReadFloat32() (f32 float32, err error) {
	err = binary.Read(self.buffer, binary.BigEndian, &f32)
	return
}

//返回缓冲区中的长度
func (self *NetPackerSt) Len() int {
	return self.buffer.Len()
}

//返回存储的字节总数
func (self *NetPackerSt) Bytes() []byte {
	return self.buffer.Bytes()
}

//重置缓冲区中的数据信息
func (self *NetPackerSt) ReSet(buf []byte) {
	self.buffer.Reset()
	if buf != nil && len(buf) > 0 {
		self.buffer.Write(buf)
	}
}
